<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item
        label="姓名"
        prop="name"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item
        label="类型"
        prop="type"
        :class="{ 'is-required': !dataForm.id }"
      >
        <el-select
          v-model="dataForm.type"
          placeholder="类型请选择"
          @change="change1($event)"
        >
          <el-option
            v-for="item in options1"
            :key="item.value"
            :label="item.label"
            :value="item.label"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { isEmail, isMobile } from "@/utils/validate";
export default {
  data() {
    return {
      parentValue: [],
      visible: false,
      roleList: [],
      options1: [
        {
          value: "选项2",
          label: "业务员",
        },
        {
          value: "选项3",
          label: "仓管员",
        },
        {
          value: "选项4",
          label: "财务员",
        },
      ],
      dataForm: {
        id: 0,
        name: "",
        type: "",
      },
      dataRule: {
        name: [{ required: true, message: "姓名不能为空", trigger: "change" }],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
      },
    };
  },
  methods: {
    init(index, id, pageIndex, pageSize, name, type) {
      // console.log(index, id, pageIndex, pageSize, name, type);
      this.dataForm.id = id || 0;
      this.$http({
        url: this.$http.adornUrl("/sys/role/select"),
        method: "get",
        params: this.$http.adornParams(),
      })
        .then(({ data }) => {
          this.roleList = data && data.code === 0 ? data.list : [];
        })
        .then(() => {
          this.visible = true;
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields();
          });
        })
        .then(() => {
          if (this.dataForm.id) {
            let str = `{"name":"${name}","type":"${type}"}`;
            var search = encodeURIComponent(str);
            this.$http({
              url: this.$http.adornUrl(`/person/list?search=${search}`),
              method: "get",
              params: this.$http.adornParams({
                currentPage: pageIndex,
                pageSize: pageSize,
              }),
            }).then(({ data }) => {
              // console.log(data.data.list[index]);
              if (data && data.code === 0) {
                this.dataForm = data.data.list[index];
              }
            });
          }
        });
    },
    change1(id) {
      // console.log(id)
      // if(this.dataForm.value1=="全部"){
      //   this.type=""
      //   return
      // }
      // this.type=this.dataForm.value1
      this.type = this.dataForm.value1 == "全部" ? "" : this.dataForm.value1;
      // console.log(this.dataForm.value1)
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          console.log(this.dataForm.name);
          let info = { Name: this.dataForm.name, Type: this.dataForm.type };
          let url = this.$http.adornUrl("/person/add");
          if (this.dataForm.id) {
            url = this.$http.adornUrl(`/person/update?id=${this.dataForm.id}`);
          }
          this.$http({
            url,
            method: "post",
            data: this.$http.adornData(info, false),
          }).then(({ data }) => {
            console.log(data);
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
